package com.ddwl.wallet.job;

import com.ddwl.common.constant.RabbitMqConstants;
import com.ddwl.common.feign.api.OrderApi;
import com.ddwl.schema.util.DateUtil;
import com.ddwl.wallet.service.OrderPayOffService;
import com.ddwl.wallet.service.UserCouponService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * 类WalletJob.java的实现描述：TODO
 *
 * @author zach
 * @Date :2020-07-25 17:00.
 */
@Component
@RequiredArgsConstructor
public class WalletJob {

    private final OrderPayOffService orderPayOffService;
    private final UserCouponService userCouponService;
    private final OrderApi orderApi;
    private final RabbitTemplate rabbitTemplate;

    /**
     * 开始执行分销订单结算任务（结算上个月的完成收货订单）
     */
    @XxlJob("distOrderPayOffJob")
    public ReturnT<String> orderPayOff(String param) {
        XxlJobLogger.log("开始执行分销订单结算任务(结算上个月的完成收货订单)");
        orderPayOffService.orderPayOffDist(DateUtil.getBeforeMonthStart(), DateUtil.getBeforeMonthEnd());
        return ReturnT.SUCCESS;
    }

    /**
     * 开始执行分销订单结算任务（自动结算已收货并且已过了售后时间的订单）
     */
    @XxlJob("orderPayOffOverAfterSaleJob")
    public ReturnT<String> orderPayOffOverAfterSale(String param) {
        XxlJobLogger.log("开始执行分销订单结算任务(自动结算已收货并且已过了售后时间的订单)");
        Integer afterSaleDate = orderApi.getRepairTime().getResult();
        if (afterSaleDate == null) {
            afterSaleDate = 8;
        }
        orderPayOffService.orderPayOffDist(null, DateUtil.getTimesNight(-(afterSaleDate + 1)));
        //orderPayOffService.orderPayOffOverAfterSale();
        return ReturnT.SUCCESS;
    }

    /**
     * 开始执行分销订单结算任务（自动结算开店佣金）
     */
    @XxlJob("orderPayOffShopApproveJob")
    public ReturnT<String> orderPayOffShopApproveJob(String param) {
        XxlJobLogger.log("开始执行分销订单结算任务(自动结算开店佣金)");
        orderPayOffService.orderPayOffShopApprove();
        return ReturnT.SUCCESS;
    }


    /**
     * 开始执行分销订单结算任务（结算所有的完成订单）
     */
    @XxlJob("distOrderNowPayOffJob")
    public ReturnT<String> orderNowPayOff(String param) {
        XxlJobLogger.log("开始执行分销订单结算任务(结算所有的完成订单)");
        orderPayOffService.orderPayOffDist(null, null);
        //orderPayOffService.orderNowPayOff();
        return ReturnT.SUCCESS;
    }

    /**
     * 开始执行分销订单结算任务（结算指定单子）
     */
    @XxlJob("distOrderNowPayOffByOrderJob")
    public ReturnT<String> orderNowPayOffByOrder(String orderId) {
        XxlJobLogger.log("开始执行分销订单结算任务(结算指定单子)");
        rabbitTemplate.convertAndSend(RabbitMqConstants.DIST_DIST_ORDER_SETTLEMENT_AMOUNT_QUEUE, orderId);
        return ReturnT.SUCCESS;
    }

    /**
     * 开始执行商户订单结算
     */
    @XxlJob("orderPayOffShop")
    public ReturnT<String> orderPayOffShop(String userId) {
        XxlJobLogger.log("开始执行商户订单结算");
        orderPayOffService.orderPayOffShop(null, null);
        // orderPayOffService.orderPayOffShop();
        return ReturnT.SUCCESS;
    }

    @XxlJob("expireCoupon")
    public ReturnT<String> expireCoupon(String param) {
        XxlJobLogger.log("开始标记优惠券过期");
        userCouponService.expireCoupon();
        return ReturnT.SUCCESS;
    }

}
